package day02;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Test2 {
	public static void main(String[] args) throws Exception {
		DocumentBuilder builder = DocumentBuilderFactory.newInstance()
				.newDocumentBuilder();
		Document doc = builder.parse(new File("../../user.xml"));
		Element root = doc.getDocumentElement();
		// 每一个node里面都是user标记
		NodeList list = root.getElementsByTagName("user");
		List<User> users = new ArrayList();
		for (int i = 0; i < list.getLength(); i++) {
			Element user = (Element) list.item(i);
			users.add(parserUser(user));
		}
		System.out.println(users.iterator().next().getName());
		System.out.println(users.iterator().next().getAge());
		System.out.println(users.iterator().next().getMoney());
	}

	private static User parserUser(Element user) {
		User u = null;
		Element name = (Element) user.getElementsByTagName("name").item(0);
		Element age = (Element) user.getElementsByTagName("age").item(0);
		Element money = (Element) user.getElementsByTagName("money").item(0);
		u = new User(name.getTextContent(), Integer.parseInt(age
				.getTextContent()), Integer.parseInt(money.getTextContent()));

		return u;

	}
}
